package com.hawk.eye.server.convert;

import com.hawk.eye.common.model.ServiceWhiteList;
import com.hawk.eye.server.domain.dto.req.WhiteListReqDTO;
import com.hawk.eye.server.domain.dto.resp.WhiteListRespDTO;
import com.hawk.eye.server.domain.po.WhiteList;
import com.hawk.eye.server.domain.vo.req.WhiteListReqVO;
import com.hawk.eye.server.domain.vo.resp.WhiteListRespVO;
import org.mapstruct.DecoratedWith;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author zd
 */
@Mapper
@DecoratedWith(WhiteListConvert.WhiteListConvertDecorator.class)
public interface WhiteListConvert {

    WhiteListConvert INSTANCE = Mappers.getMapper(WhiteListConvert.class);

    @Named(value = "to")
    WhiteListRespDTO to(WhiteList whiteList);

    @Named(value = "toServiceWhiteList")
    ServiceWhiteList toServiceWhiteList(WhiteListRespDTO whiteList);

    @IterableMapping(qualifiedByName = "to")
    List<WhiteListRespDTO> toList(List<WhiteList> dto);

    @IterableMapping(qualifiedByName = "toServiceWhiteList")
    List<ServiceWhiteList> toServiceWhiteList(List<WhiteListRespDTO> dto);

    WhiteListReqDTO to(WhiteListReqVO whiteListReqVO);


    WhiteList to(WhiteListReqDTO whiteListReqVO);

    WhiteListRespVO to(WhiteListRespDTO whiteListRespDTO);

    public abstract class WhiteListConvertDecorator implements WhiteListConvert {
        @Override
        public WhiteListRespVO to(WhiteListRespDTO whiteListRespDTO) {

            if (whiteListRespDTO == null) {
                return null;
            }

            WhiteListRespVO whiteListRespVO = new WhiteListRespVO();

            whiteListRespVO.setId(whiteListRespDTO.getId());
            whiteListRespVO.setName(whiteListRespDTO.getName());
            whiteListRespVO.setMatchKeyword(whiteListRespDTO.getMatchKeyword());
            whiteListRespVO.setServiceId(whiteListRespDTO.getServiceId());
            whiteListRespVO.setFailureTime(whiteListRespDTO.getFailureTime());
            whiteListRespVO.setServiceName(whiteListRespDTO.getServiceCnName()
                    + "(" + whiteListRespDTO.getServiceZnName() + ")");
            whiteListRespVO.setIsFailure(whiteListRespDTO.getIsFailure());
            return whiteListRespVO;
        }
    }
}